package tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

import logController.EnumMessageType;
import logController.Logger;

/**
 * @date Apr. 24, 2012
 * @author Francisco J. Martínez Wrapper class for operations with files
 */
public class FileManager {

	/**
	 * 
	 * @param file Valid descriptor file
	 * @return A String with the contains of the file.
	 */
	public static String loadFile(File file) {
		Logger.addMessage(EnumMessageType.DEBUG_INFO, "Opening: " + file.getPath());
		String res = loadText(file);
		return res;
	}
	
	/**
	 * Calls the method loadFile(File)
	 * @param path Path to file
	 * @param name name of file
	 * @return A String with the child name
	 */
	// Hay que probarlo, no estoy seguro de que funcione
	public static String loadFile(String path, String name){
		File file = new File(path,name);
		return loadFile(file);
	}

	/**
	 * Read the properties from file
	 * @param file
	 * @return A Properties object
	 */
	public static Properties readProperties(File file){
		Properties result = new Properties();
		try {
			FileInputStream in = new FileInputStream(file);
			result.load(in);
			in.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;		
	}
	
	/**
	 * Calls readProperties(File)
	 * @param path
	 * @param name
	 * @return
	 */
	public static Properties readProperties(String path, String name){
		File file = new File(path,name);
		return readProperties(file);
	}
	
	/**
	 * 
	 * @param prop
	 * @param name
	 */
	static public void writeProperties(Properties prop, String name){
		try {
			FileOutputStream out = new FileOutputStream(name);
			prop.store(out,"No description available");
			out.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @param file
	 * @param text
	 */
	static public void writeFile(File file, String text){
		try {
			FileWriter out = new FileWriter(file);
			Logger.addMessage(EnumMessageType.DEBUG_INFO, "Writing the file");
			out.write(text);
			out.close();
			Logger.addMessage(EnumMessageType.DEBUG_INFO, "Closing file descriptor");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Open a existing file and read the content Note! Maybe this method is far
	 * to be an optimal solution, by the way it's works! If u want one better...
	 * do it your self, bitch!
	 * 
	 * @return A String with the text file
	 */
	private static String loadText(File f) {
		// ...checks on aFile are elided
		StringBuilder contents = new StringBuilder();

		try {
			// use buffering, reading one line at a time
			// FileReader always assumes default encoding is OK!
			BufferedReader input = new BufferedReader(new FileReader(f));
			try {
				String line = null; // not declared within while loop
				/*
				 * readLine is a bit quirky : it returns the content of a line
				 * MINUS the newline. it returns null only for the END of the
				 * stream. it returns an empty String if two newlines appear in
				 * a row.
				 */
				while ((line = input.readLine()) != null) {
					contents.append(line);
					contents.append(System.getProperty("line.separator"));
				}
			} finally {
				input.close();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}

		return contents.toString();
	}
}
